<?php
define("INC",1);
class sourceAction extends CommonAction {
    function index(){
        $uid=$this->U['uid'];
        import("@.ORG.Page");
        $source=M('sources');
        $where=array(
          "suid"=>$uid,
           "status"=>1
        );
        if(is_numeric($this->_param("stype")) && $this->_param("stype")!=-1){
            $where['stype']=$this->_param("stype");
        }
        if($wd=$this->_param("wd")){
            $where['sname']=array("like","%$wd%");
        }
        if(($st=$this->_param("st")) && ($et=$this->_param("et"))){
            $stime=  strtotime($st);
            $etime=  strtotime($et);
            if($stime<$etime){
                $where["stime"]=array('between',array($stime,$etime));
            }
        }
        $count=$source->field("sid")->where($where)->count();
        $page=new Page($count,30);
        $show=$page->show();
        $slist=$source->where($where)->limit($page->firstRow . ',' . $page->listRows)->order('sid desc')->select();
        $stype=array("论坛","博客","问答","评论","分享","网盘","视频");
        $this->assign("stype",$stype);
        $this->assign("slist",$slist);
        $this->assign("show",$show);
        $this->display();
    }
    
    function add(){
        if(!$this->isPost()){
            $this->display();
        }else{
            $source=M('sources');
            $data=$_POST;
            $data['stime']=time();
            $data['suid']=  $this->U['uid'];
            if($source->create($data)){
                if($source->add()){
                    exit("添加成功,<a href=# onclick='parent.window.location.reload()'>确定</a>");
                }else{
                    exit('出错：'.$source->getDbError());
                }
            }else{
                exit($source->getError());
            }
        }
    }
    
    function del(){
        $sid=$this->_param("sid");
        $source=M('sources');
        $source->where(array("sid"=>$sid))->save(array("status"=>"0"));
        $this->ajaxReturn(array("status"=>"1"));
    }
    
    function edit(){
        $sid=$this->_param("sid");
        $source=M('sources');
        $uid=$this->U['uid'];
        if(!$this->isPost()){
            $sinfo=$source->where(array("sid"=>$sid,"suid"=>$uid))->find();
            if(!$sinfo){
                exit('非法请求');
            }
            $this->assign("sinfo",$sinfo);
            $this->display();
        }else{
            $data=$_POST;
            if(!$data['sid']){
                exit('修改失败');
            }
            $data['stime']=time();
            $data['suid']=  $this->U['uid'];
            if($source->create($data)){
                if($source->save()){
                    exit("修改成功,<a href=# onclick='parent.window.location.reload()'>确定</a>");
                }else{
                    exit('出错：'.$source->getDbError());
                }
            }else{
                exit($source->getError());
            }
        }
    }
    
}
